﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChampionsLeagueBets.Domain;
using ChampionsLeagueBets.Infrastructure;
using ChampionsLeagueBets.Web.Mvc.Authentication;
using ChampionsLeagueBets.Web.Mvc.Common;
using ChampionsLeagueBets.Web.Mvc.Controllers.ViewModels;
using SharpArch.NHibernate.Contracts.Repositories;
using SharpArch.NHibernate.Web.Mvc;
using MvcContrib;

namespace ChampionsLeagueBets.Web.Mvc.Controllers
{
    public class UnitOfAccountsController : Controller
    {
		/// <summary>
		/// Dostep do bazy danych.
		/// </summary>
		private readonly IExRepository<UnitOfAccount> _unitOfAccountRepository;

		public UnitOfAccountsController(IExRepository<UnitOfAccount> unitOfAccountRepository)
    	{
    		_unitOfAccountRepository = unitOfAccountRepository;
    	}

    	public ActionResult Index()
    	{
			if (!RightPrincipal.CurrentUser.IsAdmin) return this.RedirectToAction<HomeController>(c => c.NoRights());

    		IList<UnitOfAccount> list = _unitOfAccountRepository.GetAll();
            return View(list);
        }

        public ActionResult Create()
        {
			if (!RightPrincipal.CurrentUser.IsAdmin) return this.RedirectToAction<HomeController>(c => c.NoRights());

            return View();
        } 

        [HttpPost]
		[Transaction]
		[ValidateAntiForgeryToken]
        public ActionResult Create(UnitOfAccount unitOfAccount)
        {
			if (!RightPrincipal.CurrentUser.IsAdmin) return this.RedirectToAction<HomeController>(c => c.NoRights());

            try
            {
                if(ViewData.ModelState.IsValid)
                {
                	_unitOfAccountRepository.SaveOrUpdate(unitOfAccount);

					TempData[ActionConfirmation.TempDataKey] = ActionConfirmation.CreateSuccess("Poprawnie zapisano dane jednostki rozliczeniowej.");
					return this.RedirectToAction(c => c.Index());
                }

				TempData[ActionConfirmation.TempDataKey] = ActionConfirmation.CreateError("Wystąpił błąd podczas zapisywania danych jednostki rozliczeniowej.");
				return View();
            }
            catch
            {
				TempData[ActionConfirmation.TempDataKey] = ActionConfirmation.CreateError("Wystąpił błąd podczas zapisywania danych jednostki rozliczeniowej.");
                return View();
            }
        }
        
        public ActionResult Edit(int id)
        {
			if (!RightPrincipal.CurrentUser.IsAdmin) return this.RedirectToAction<HomeController>(c => c.NoRights());

        	UnitOfAccount unitOfAccount = _unitOfAccountRepository.Get(id);
            return View(unitOfAccount);
        }

        [HttpPost]
		[Transaction]
		[ValidateAntiForgeryToken]
        public ActionResult Edit(int id, UnitOfAccount unitOfAccount)
        {
			if (!RightPrincipal.CurrentUser.IsAdmin) return this.RedirectToAction<HomeController>(c => c.NoRights());

            try
            {
				if (ViewData.ModelState.IsValid)
				{
					UnitOfAccount editable = _unitOfAccountRepository.Get(id);
					editable.Name = unitOfAccount.Name;
					editable.ShortName = unitOfAccount.ShortName;
					_unitOfAccountRepository.SaveOrUpdate(editable);

					TempData[ActionConfirmation.TempDataKey] = ActionConfirmation.CreateSuccess("Poprawnie zapisano dane jednostki rozliczeniowej.");
					return this.RedirectToAction(c => c.Index());
				}

				TempData[ActionConfirmation.TempDataKey] = ActionConfirmation.CreateError("Wystąpił błąd podczas zapisywania danych jednostki rozliczeniowej.");
				return View();
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
			if (!RightPrincipal.CurrentUser.IsAdmin) return this.RedirectToAction<HomeController>(c => c.NoRights());

        	UnitOfAccount unitOfAccount = _unitOfAccountRepository.Get(id);
			_unitOfAccountRepository.Delete(unitOfAccount);
			_unitOfAccountRepository.DbContext.CommitChanges();

			return PartialView("_UnitOfAccountsList", _unitOfAccountRepository.GetAll());
        }

		public ActionResult UnitOfAccountsList(UnitOfAccount unitOfAccount, string name)
		{
			if (!RightPrincipal.CurrentUser.IsAdmin) return this.RedirectToAction<HomeController>(c => c.NoRights());

			var unitOfAccountsListViewModel = new UnitOfAccountsListViewModel();
			unitOfAccountsListViewModel.ControlName = name;

			IList<UnitOfAccount> unitOfAccounts = _unitOfAccountRepository.GetAll();
			foreach (var item in unitOfAccounts)
			{
				unitOfAccountsListViewModel.Items.Add(new SelectListItem { Selected = unitOfAccount != null && unitOfAccount.Id == item.Id, Text = item.Name, Value = item.Id.ToString() });
			}

			return PartialView("_UnitOfAccount", unitOfAccountsListViewModel);
		}
    }
}
